package com.hunlian.makelove.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.adapter.ContactAdapter;
import com.hunlian.makelove.bean.ContactModel;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.ContactServiceVisit;
import com.hunlian.makelove.tools.ACache;
import com.hunlian.makelove.tools.MyToast;

import java.util.ArrayList;

public class ContactActivity extends BaseActivity {

    private ContactActivity mActivity;
    private String mTitle;
    private ListView lvContent;
    private ContactAdapter contactAdapter;
    private Button btnReload;
    private RelativeLayout rlNoNet;
    private ArrayList<ContactModel.BeansBean> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        mActivity = this;
        mTitle = "联系我们";

        contactAdapter = new ContactAdapter(this);

        arrayList = (ArrayList<ContactModel.BeansBean>)ACache.get(AppApplication.getAppContext()).getAsObject("ArrayList<ContactModel>");
        if(arrayList==null||arrayList.size()==0){
            arrayList = new ArrayList<>();
        }
        contactAdapter.setList(arrayList);

        RelativeLayout ll_title = (RelativeLayout) this.findViewById(R.id.ll_title);
        TextView mTvTitle = (TextView) ll_title.findViewById(R.id.title);
        mTvTitle.setText(mTitle);
        Button btnR = (Button) ll_title.findViewById(R.id.btn_nav_right);
        btnR.setVisibility(View.GONE);
        ImageButton mBtnBack = (ImageButton) ll_title.findViewById(R.id.back);
        mBtnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mActivity.finish();
            }

        });

        rlNoNet = (RelativeLayout) findViewById(R.id.no_net);
        btnReload = (Button)findViewById(R.id.btnReLoad);
        lvContent = (ListView)findViewById(R.id.lv_content);
        lvContent.setAdapter(contactAdapter);

        setListener();

        if(arrayList.size()==0){
            //请求网络
            getContactInfo();
        }
    }

    private void setListener(){
        btnReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getContactInfo();
            }
        });
    }


    /**
     * 获取联系人信息
     * @author zealjiang
     * @time 2016/8/30 9:02
     */
    private void getContactInfo() {
        if (!AppApplication.networkAvailable) {
            MyToast.showShort("没有网络");
            return;
        }

        ContactServiceVisit visit = new ContactServiceVisit(mActivity);
        visit.setParamters();
        //System.out.println("获取联系人请求参数：" + visit.getParamter());
        visit.getResult(new NetVisitCallback<ArrayList<ContactModel.BeansBean>,Object>(mActivity){

            @Override
            public void onSuccess(ArrayList<ContactModel.BeansBean> bean) {

                if(bean==null)return;
                rlNoNet.setVisibility(View.GONE);
                lvContent.setVisibility(View.VISIBLE);
                ACache.get(AppApplication.getAppContext()).put("ArrayList<ContactModel>",bean);
                contactAdapter.setList(bean);
                contactAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(String errorEvent, String message,Object obj) {
                lvContent.setVisibility(View.GONE);
                rlNoNet.setVisibility(View.VISIBLE);
            }

        });
    }
}
